package com.qingke.easyjava.jdbc.command.impl;

import com.qingke.easyjava.jdbc.app.BeSuperApplication;
import com.qingke.easyjava.jdbc.app.BeSuperDao;
import com.qingke.easyjava.jdbc.command.SystemCommand;
import com.qingke.easyjava.jdbc.pojo.Player;
import com.qingke.easyjava.jdbc.util.QingkeConsole;

public class LoginCommand extends SystemCommand {

    @Override
    public void execute() {
        BeSuperDao dao = new BeSuperDao();
        
        String username = QingkeConsole.askUserInput("请输入用户名");
        // check username 
        while (!dao.isUsernameExists(username)) {
            QingkeConsole.println("用户名不存在！");
            username = QingkeConsole.askUserInput("请输入用户名");
        }

        String password = QingkeConsole.askUserInput("请输入密码");
        
        Player player = dao.login(username, password);
        
        if (player == null) {
            QingkeConsole.println("登录失败！用户名／密码不正确");
            return;
        }

        QingkeConsole.println("欢迎你回来，" + player.getName());
        BeSuperApplication.getInstance().setPlayerProfile(player);
    }

}
